//
//  PracticesViewController.m
//  TableView03
//
//  Created by 大宝 刘 on 11-11-15.
//  Copyright (c) 2011年 北京帷幄昊合数字娱乐科技有限公司. All rights reserved.
//

#import "PracticesViewController.h"
#import "SzjCell.h"

@implementation PracticesViewController

@synthesize dataArray = _dataArray;
@synthesize myTableView = _myTableView;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // 初始化三字经内容文件
    NSString *path = [[NSBundle mainBundle]pathForResource:@"sanzijing" ofType:@"plist"];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];
    
    // 设置表格背景色透明
    [self.myTableView setBackgroundColor:[UIColor clearColor]];
    
    // 初始化三字经音频文件
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"szj" ofType:@"mp3"];
    NSURL *soundUrl = [[NSURL alloc] initFileURLWithPath:soundPath];
    
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
    
	[player prepareToPlay];
    player.numberOfLoops = -1;
    [player play];
    
    soundUrl = nil;
}

- (void)viewDidUnload
{
    [self setMyTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    // 释放数组资源
    self.dataArray = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - UITableView delegate methods

// 表格中的区段数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// 区段中的数据行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"一共有 %d 条记录", [self.dataArray count]);
    
    return [self.dataArray count];
}

// 初始化单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndentifier = @"cell";
    
    
    SzjCell *cell = (SzjCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if(cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]
                        loadNibNamed:@"SzjCell" 
                        owner:self options:nil] ;
        cell = [nib objectAtIndex:0];
    }

    
    NSDictionary *item = [self.dataArray objectAtIndex:indexPath.row];
    NSString *content = [item objectForKey:@"text"];
    NSString *pinyin = [item objectForKey:@"pinyin"];
    NSString *description = [item objectForKey:@"description"];
    
    NSLog(@"当前内容为： %@ 第[%d]条", content, indexPath.row);

    cell.pinyinLabel.text = pinyin;
    cell.contentLabel.text = content;
    cell.descriptionLabel.text = description;
    
    return cell;
}


@end
